/*
	Copyright (C) 2008 by Jean-FranÁois Geyelin
	jfgeyelin@gmail.com

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the
	Free Software Foundation, Inc.,
	51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/


#include "globals.h"
#include "safeFileIO.h"
#include "SurfaceHandler.h"
#include "settings.h"

//ScreenClass *screen;

ScreenClass *ScreenClass::screenClassInstance = NULL;


ScreenClass *ScreenClass::getInstance(void) {
    if (screenClassInstance!=NULL) {
        return screenClassInstance;
    }
    screenClassInstance = new ScreenClass();
}

void ScreenClass::deleteInstance(void) {
    if (screenClassInstance==NULL) {
        return;
    }
    delete screenClassInstance;
    screenClassInstance = NULL;
}


ScreenClass::ScreenClass(void) {
    if (SDL_Init(SDL_INIT_TIMER|SDL_INIT_VIDEO| SDL_INIT_JOYSTICK)==-1) {
        LOG_ERROR("failed to initialize SDL\n");
    }
    else {

    }
	/*freopen( "CON", "w", stdout );
	freopen( "CON", "w", stderr );
*/

#ifndef __APPLE__
    windowIcon = SDL_LoadBMP_safe("graphics/icon.bmp");
#endif

    if (settingsExists()) {
        createNewScreen();
    }
    else
    {
        screen = SDL_SetVideoMode(500, 500, SCREEN_BPP, SDL_HWSURFACE | SDL_DOUBLEBUF/* | SDL_NOFRAME*/);
        if (screen == NULL) {
            LOG_ERROR("failed to set video mode\n");
        }
        createDefaultSettingsFile();
        createNewScreen();
    }
}

ScreenClass::~ScreenClass(void) {
    SDL_Quit();
    refreshScreenSurface();
}

SDL_Surface *ScreenClass::getScreen(void) {
    return screen;
}

void ScreenClass::setScreen(SDL_Surface *s) {
    /*screen = s;
    refreshScreenSurface();*/
}


/*void loadSettings(Settings *);
int getResolutionWidth(int);
int getResolutionHeight(int);
*/

#include "settings.h"

void initColorsGlobals(void);



void ScreenClass::createNewScreen(void) {

	loadSettings(&settings);
    SCREEN_WIDTH = getResolutionWidth(settings.resolution);
    SCREEN_HEIGHT = getResolutionHeight(settings.resolution);
    /*#ifdef NO_SCREEN
    SDL_FreeSurface(screen);
    screen = SDL_CreateRGBSurface(SDL_SWSURFACE|SDL_SRCCOLORKEY, SCREEN_WIDTH, SCREEN_HEIGHT, 32,0,0,0,0);

    #else*/

    //| SDL_NOFRAME | SDL_RESIZABLE
    screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, /*SDL_RESIZABLE | SDL_NOFRAME |*/ SDL_HWSURFACE | SDL_DOUBLEBUF | (settings.fullScreen?SDL_FULLSCREEN:0));
    if (screen == NULL)
    LOG_ERROR("failed to set new video mode\n");

#ifndef __APPLE__
    SDL_SetColorKey(windowIcon, SDL_SRCCOLORKEY, SDL_MapRGB( windowIcon->format, 255, 0, 255 ));

    SDL_WM_SetIcon(windowIcon, NULL);
#endif
    SDL_WM_SetCaption("Kill Mario", NULL);
    //#endif

    refreshScreenSurface();
    //SurfaceHandler::colorScreen(20,20,20);
    initColorsGlobals();
}

void ScreenClass::refreshScreenSurface(void) {
    screenSurface = screen;
}
